#include<stdio.h>
#include"util.h"
#include"symbol.h"
#include"env.h"



struct E_enventry* E_varEntry(struct valtype* var)
{
	struct E_enventry* ptr = (struct E_enventry*)sec_malloc(sizeof(struct E_enventry));
	ptr->kind = E_var;
	ptr->u.var = var;

	return ptr;
}
struct E_enventry* E_funEntry(struct CM_vardeclist* list, struct valtype* res, struct CM_stmtlist* stmt)
{
	struct E_enventry* ptr = (struct E_enventry*)sec_malloc(sizeof(struct E_enventry));
	ptr->kind = E_fun;
	ptr->u.fun = (struct funfield*)sec_malloc(sizeof(struct funfield));
	ptr->u.fun->params = list;
	ptr->u.fun->result = res;
	ptr->u.fun->stmt = stmt;

	return ptr;
}



struct table* E_base_type_env()
{
	struct symbol* sym;
	struct table* t = newtable();
	
	//内置了 int, char, double, void 四个基础类型
	
	sym = insertSymbol("int");
	Symtable_insert(t, sym, cm_ty_tyint());
	sym = insertSymbol("double");
	Symtable_insert(t, sym, cm_ty_tydouble());
	sym = insertSymbol("char");
	Symtable_insert(t, sym, cm_ty_tychar());
	sym = insertSymbol("void");
	Symtable_insert(t, sym, cm_ty_tyvoid());

	return t;
}

struct table* E_base_value_env()
{
	return newtable();
}

void showEnvType(void* key, void* value)
{
	struct symbol* sym = (struct symbol*)key;
	struct type* ty = (struct type*)value;

	printf("type name: %s  and type is ", sym->name);
	switch(ty->kind){
		case var_int:
			printf("var_int\n");
			break;
		case var_double:
			printf("var_double\n");
			break;
		case var_char:
			printf("var_char\n");
			break;
		case var_void:
			printf("var_void\n");
			break;
		case var_struct:
			printf("var_struct\n");
			break;
		default:
			printf("undefined type! WRONG\n");
	}

}

void showEnvValue(struct E_enventry* e)
{
	struct valtype* var = NULL;
	if(e->kind == E_var){
		var = e->u.var;
		showValue(var);
	}else if(e->kind == E_fun){
		printf("delay.\n");
	}else{
		printf("undeclare variable.\n");
	}
}
